<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>我的电商系统</title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>public/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>public/css/bootstrap-theme.min.css">
	<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>public/css/fornt.css">
</head>
<body id="shopcart">
<div class="container"> 
	<div class="row">
		<div class="col-md-3 nav-left">
			<a href="<?php echo site_url();?>"><img id="logo"  src="<?php echo base_url();?>public/img/logo.jpg" /></a>
		</div>
		<div class="col-md-9"><img style="height:50px;margin-top:20px" class="pull-right" src="<?php echo base_url();?>public/img/shop_step_1.jpg" /></div>
		
	</div>
</div>
<br>
<div class="container">
	<div class="row">
		<div class="col-md-12">
			<form action="" method="post">
				<table cellpadding="0" cellspacing="0" class="shopcart_table">
					<tr class="shopcart_table_item">
						<th class="item">商品</th>
						<th class="price">单价</th>
						<th class="num">数量</th>
						<th class="sum">小计(元)</th>
						<th class="operate">操作</th>
					</tr>
				<?php 
					$sum=0;
					foreach($user_cata as $usercart){
					$p=$this->Product_model->get_one_product($usercart->product_id);
				?>
					<tr class="tr">
						<input type="hidden" name="product_id" class="product_id" value="<?php echo $usercart->product_id?>" />
						<td>
							<span class="checkbox pull-left">
							    <label><input class="is_select" name="ids[]" type="checkbox" value="<?php echo $usercart->id?>"></label>
							</span>
							<img src="<?php echo base_url().$p->img?>" class="pull-left" />
							<p><a class="title" href="<?php echo site_url('Product/detail/'.$p->id)?>"><?php echo $p->title;?></a></p>
						</td>
						<td class="price">￥<span><?php echo $p->price;?></span></td>
						<td class="cart">
							<span class="minus"><i class="glyphicon glyphicon-minus"></i></span>
							<input class="num" name="num" type="text" value="<?php echo $usercart->num?>">
							<span class="plus"><i class="glyphicon glyphicon-plus"></i></span>
						</td>
						<td width="120px" class="sum">¥<span ><?php echo $p=intval($p->price) * intval($usercart->num);?></span></td>
						<td width="100px"><a href="<?php echo site_url('/shopping/shopcart_delete/').$usercart->id?>">删除</a></td>
					</tr>
					<?php $sum=$sum+$p;?>
					<?php }?>
					<tr>
						<td colspan="10">
							<div class="checkbox" style="text-align:left;display:block">
							    <label><input id="checkall" type="checkbox">全选</label>
							    <a id="delete_all" href="#" >删除所选商品</a>
							</div>
							<p style="text-align:right;padding-right:25px;color:red;">合计：￥<?php echo $sum;?></p>
						</td>
					</tr>
				</table>
				<button id="shop_create" type="submit" class="btn btn-danger pull-right" style="margin-top:20px">去结算</button>
			</form>
		</div>
	</div>
</div>
</body>
	<script type="text/javascript" src="<?php echo base_url();?>public/js/jquery.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#checkall").click(function () { 
                $("input:checkbox").prop("checked", this.checked);  
            }); 
			$('#delete_all').click(function(){
				var ids=[];
				$('.is_select').each(function(i,item){
					if($(item).prop('checked')){
						ids.push($(item).val());
					}
				});
				if(ids.length==0){
					alert('没有选择任何商品');
				}else{
					$.post("<?php echo site_url('shopping/cartdels')?>",
						{'ids':ids},function(data){
						if(data=='ok'){
							location.reload();
						}
					})
				}
				return false;
			});
			$('.plus').click(function(){
				var num =$(this).parents('.cart').find('.num').val();
				var id=$(this).parents('.tr').find('.is_select').val();
				num=parseInt(num)+1;
				$.get("<?php echo site_url('shopping/update_cart_num')?>",{'id':id,'num':num},function(data){
					if(data=='ok'){
						location.reload();
					}else{
						alert('商品数量至少为一件.');
					}
				})
			});
			$('.minus').click(function(){
				var num =$(this).parents('.cart').find('.num').val();
				var id=$(this).parents('.tr').find('.is_select').val();
				num=parseInt(num)-1;
				$.get("<?php echo site_url('shopping/update_cart_num')?>",{'id':id,'num':num},function(data){
					if(data=='ok'){
						location.reload();
					}else{
						alert('商品数量至少为一件.');
					}
				})
			});
			$('.num').blur(function(){
				var num =$(this).val();
				var id=$(this).parents('.tr').find('.is_select').val();
				$.get("<?php echo site_url('shopping/update_cart_num')?>",{'id':id,'num':num},function(data){
					if(data=='ok'){
						location.reload();
					}else{
						alert('商品数量至少为一件.');
					}
				})
			});
			$('#shop_create').click(function(){
				var data=[];
				$('.is_select').each(function(i,item){
					if($(item).prop('checked')){
						var element={
							'product_id':$(this).parents('.tr').find('.product_id').val(),
							'num':$(this).parents('.tr').find('.num').val()
						}
						data.push(element);
					}
				});
				// console.log(data);
				// console.log(data.length);
				if(data.length==0){
					alert('请选择一件商品');
				}else{
					$.post("<?php echo site_url('shopping/createorder')?>",{'data':data},function(data){
						if(data=='ok'){
							window.location.href="<?php echo site_url('/shopping/shoporder')?>";
						}else{
							alert(data);
						}
					});
				}
				return false;
			})
		})
	</script>
</html>